"""
k-均值
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.cluster as sc  # 聚类模块
import sklearn.metrics as sm

data = pd.read_csv('multiple3.txt', header=None, names=['x1', 'x2'])
print(data)

# plt.scatter(data['x1'],data['x2'],s=50,color='red')
# plt.show()

model = sc.KMeans(n_clusters=4)
model.fit(data)

labels = model.labels_
print(labels)

plt.scatter(data['x1'], data['x2'], s=50, c=labels, cmap='brg')
plt.colorbar()

centers = model.cluster_centers_
print(centers)
plt.scatter(centers[:, 0], centers[:, 1], s=500, c='black', marker='+')
plt.show()
# data:样本 labels：标签 sample_size：样本数量 metric：欧式距离度量
score = sm.silhouette_score(data, labels, sample_size=len(data), metric='euclidean')
print(score)
